from fastapi import APIRouter, Depends, Query
from pydantic import BaseModel
from sqlalchemy.orm import Session
from typing import Optional
from services.official import create_contact, get_contact_list, get_contact_by_id
from core.database import get_db
from utils.result import Result

router = APIRouter(prefix="/official", tags=["official"])

# 定义请求模型
class ContactCreateRequest(BaseModel):
    name: str
    phone: str
    description: Optional[str] = None
@router.post("/add", response_model=Result)
async def create_official_contact(
    contact_request: ContactCreateRequest,
    db: Session = Depends(get_db)
):
    """
    创建新的联系信息
    """
    return create_contact(db,   contact_request.name,
        contact_request.phone,
        contact_request.description)

@router.get("/list", response_model=Result)
async def read_official_contacts(
    page: int = Query(0, ge=0),
    size: int = Query(100, ge=1, le=1000),
    db: Session = Depends(get_db)
):
    """
    获取联系信息列表（支持分页）
    """
    return get_contact_list(db, page, size)

@router.get("/{contact_id}", response_model=Result)
async def read_official_contact(contact_id: int, db: Session = Depends(get_db)):
    """
    根据ID获取联系信息详情
    """
    return get_contact_by_id(db, contact_id)
